package operation;

import book.Book;
import booklist.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-04-10
 * Time: 15:50
 */
public class DeleteOperation implements Operation {
    @Override
    public void work(BookList booklist) {
        System.out.println("删除书籍");
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要删除的书籍名称：");
        //先查找书籍
        String name = scan.nextLine();
        int cursize = booklist.getBooksize();
        int i=0;
        int index = 0;
        for( ; i<cursize; i++) {
            Book book = booklist.getpos(i);
            if( book.getName().equals(name) ) {
                index = i;
                break;
            }
        }
        if( i==cursize) {
            System.out.println("要删除书籍不存在");
            return ;
        }
        //从index位置开始进行覆盖
        for(int j = index; j<cursize-1;j++) {
            booklist.setbook(j,booklist.getpos(j+1));
        }
        System.out.println("删除成功！");
        booklist.setBooksize(cursize-1);
    }

}
